package com.hzy.user;

import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Data
@Slf4j
public class User {

    private String id;
    private String username;
    private String nickname;
    private String password;
    private String user;


    @Builder
    @Data
    protected static class Product {
        String id;
        String merchantCode;
        String name;
        String type;
        String scene;
        String serviceName;
    }

    private static final String[] productId = {"ab3a1ddd-6577-11e9-bb9a-0205857feb80", "b27bde4f-6577-11e9-bb9a-0205857feb80", "b726c800-6577-11e9-bb9a-0205857feb80"};

    private static List<Product> productList = new ArrayList<>();

    public static void main(String[] args) throws IOException {

        productList.add(Product.builder()
                .id(productId[0])
                .merchantCode("SHENGDA")
                .name("盛大洗车券")
                .type("VOUCHER")
                .scene("BEAUTY")
                .serviceName("ORDINARYWASH")
                .build());

//        productList.add(Product.builder()
//                .id(productId[1])
//                .merchantCode("SHENGDA")
//                .name("盛大洗车券")
//                .type("VOUCHER")
//                .scene("BEAUTY")
//                .serviceName("EXQUISITEWASH")
//                .build());

        productList.add(Product.builder()
                .id(productId[2])
                .merchantCode("SHENGDA")
                .name("盛大打蜡券")
                .type("VOUCHER")
                .scene("BEAUTY")
                .serviceName("WAXING")
                .build());

        StringBuilder sbToSaveProducts = new StringBuilder("INSERT INTO product(id, merchant_code, name, type, scene, service_name)\nVALUES");
        for (int i = 0; i < productList.size(); i++) {
            Product product = productList.get(i);
            sbToSaveProducts
                    .append("(\"")
                    .append(product.getId())
                    .append("\", \"")
                    .append(product.getMerchantCode())
                    .append("\", \"")
                    .append(product.getName())
                    .append("\", \"")
                    .append(product.getType())
                    .append("\", \"")
                    .append(product.getScene())
                    .append("\", \"")
                    .append(product.getServiceName())
                    .append("\")")
                    .append(",")
                    .append("\n");
        }


        String saveProductSQL = sbToSaveProducts.toString();

        log.info("save product SQL:" + saveProductSQL.substring(0, saveProductSQL.length()-2));

        XSSFWorkbook xssfWorkbook = new XSSFWorkbook(new FileInputStream("D:/盛大门店信息UAT.xlsx"));
        XSSFSheet xssfSheet = xssfWorkbook.getSheetAt(0);
        int lastRowNum = xssfSheet.getLastRowNum();


        StringBuilder sbToSaveSku = new StringBuilder("INSERT INTO sku(id ,product_id, outlet_id, merchant_source, price, count, city, type, description, valid_days)\nVALUES ");

        for (int i = 0; i <= lastRowNum; i++) {

            XSSFRow row = xssfSheet.getRow(i);

            String id = row.getCell(0).getStringCellValue();
            String city = row.getCell(4).getStringCellValue();

            for (int j = 0; j < productList.size(); j++) {

                Info info = getInfo(city, productList.get(j).getServiceName());

                sbToSaveSku.append("(UUID(), \"")
                        .append(productList.get(j).getId())
                        .append("\", \"")
                        .append(id)
                        .append("\", \"")
                        .append("SHENGDA")
                        .append("\", \"")
                        .append(info.getPrice())
                        .append("\", ")
                        .append("10000")
                        .append(", \"")
                        .append(city)
                        .append("\", \"")
                        .append(info.getType())
                        .append("\", \"")
                        .append(info.getDescription())
                        .append("\", ")
                        .append("180")
                        .append(")")
                        .append(",")
                        .append("\n");
            }
        }

        String skuSQL = sbToSaveSku.toString();
        log.info("save sku SQL:" + skuSQL.substring(0, skuSQL.length()-2));
    }

    @Builder
    @Data
    protected static class Info {
        int price;
        String description;
        String type;
    }

    private static Info getInfo(String city, String serviceName) {
//        log.info("city===>" + city);
//        log.info("serviceName===>" + serviceName);

        if (city.equals("深圳市")) {
            if (serviceName.equals("ORDINARYWASH")) {
                return Info.builder().price(35).description("盛大普通洗车-全车型").type("SEVEN_SEATS").build();
            }
            if (serviceName.equals("WAXING")) {
                return Info.builder().price(130).description("盛大手工打蜡-全车型").type("SEVEN_SEATS").build();
            }
        }
        if (city.equals("广州市")) {
            if (serviceName.equals("ORDINARYWASH")) {
                return Info.builder().price(35).description("盛大普通洗车-全车型").type("SEVEN_SEATS").build();
            }
            if (serviceName.equals("WAXING")) {
                return Info.builder().price(110).description("盛大手工打蜡-全车型").type("SEVEN_SEATS").build();
            }
        }
        if (city.equals("佛山市")) {
            if (serviceName.equals("WAXING")) {
                return Info.builder().price(90).description("盛大手工打蜡-5座").type("FIVE_SEATS").build();
            }
            if (serviceName.equals("ORDINARYWASH")) {
                return Info.builder().price(30).description("盛大普通洗车-5座").type("FIVE_SEATS").build();
            }
        }
        if (city.equals("东莞市")) {
            if (serviceName.equals("WAXING")) {
                return Info.builder().price(90).description("盛大手工打蜡-5座").type("FIVE_SEATS").build();
            }
            if (serviceName.equals("ORDINARYWASH")) {
                return Info.builder().price(30).description("盛大普通洗车-5座").type("FIVE_SEATS").build();
            }
        }

        return null;
    }

}
